package org.mx.test.service.impl;

import org.mx.dal.service.GeneralDictAccessor;
import org.mx.test.entity.AccountEntity;
import org.mx.test.entity.RoleEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@SuppressWarnings("javadoc")
@Component
public class AccountRoleServiceImpl {
	private GeneralDictAccessor accessor;
	
	@Autowired
	public AccountRoleServiceImpl(@Qualifier("generalDictAccessor") GeneralDictAccessor accessor) {
		this.accessor = accessor;
	}
	
	@Transactional(readOnly = true)
	public AccountEntity getAccount(String code) {
		AccountEntity account = accessor.getByCode(code, AccountEntity.class);
		if (account != null && account.getRoles() != null) {
			account.getRoles().size();
		}
		return account;
	}
	
	@Transactional(readOnly = true)
	public RoleEntity getRole(String code) {
		RoleEntity role = accessor.getByCode(code, RoleEntity.class);
		if (role != null && role.getAccounts() != null) {
			role.getAccounts().size();
		}
		return role;
	}
}
